pimpas.Format = Class.create( );
pimpas.Extends(pimpas.Format, pimpas.Object);

pimpas.Extends(pimpas.Format,
	{
		initialize: function() {
			this.id = pimpas.globalID++;
			this.log = new pimpas.Log();
			this.listener = new pimpas.Listener();
			pimpas.Extends(this.listener,
				{
					callBack: function(event, el, mask, listener, apply) {
						event = pimpas.DOM.Event.fixEvent(event);
						var id = ((el.id==null) ? el.name : el.id);
						this.log.debug("[Callback Format]: Apply? "+apply+", Field: "+el+", Mask: "+mask);
						if(pimpas.DOM.Event.getKeyPressed(event) == 8 || pimpas.DOM.Event.getKeyPressed(event) == 46) {
							// backspace or delete
							return;
						}
						if(listener != null) {
							this.log.debug('[Callback Format]: Calling listener BEFORE!');
							listener.callBack.bind(listener, pimpas.Format.time.BEFORE, el, mask)();
						}
						if(!apply) {
							this.log.debug("[Callback Format]: Cleaning field...");
							var stuffs = new Array();
							for(var i=0;i<mask.length;i++)
								if(mask.charAt(i) != '#')
									if(!stuffs.have(mask.charAt(i)))
										stuffs.push(mask.charAt(i));
							var tmp = null;
							var value = pimpas.DOM.getObjectValue( el );
							while(stuffs.length > 0) {
								tmp = stuffs.pop();
								while(value.indexOf(tmp)>=0)
									value = value.replace(tmp, "");
							}
							pimpas.DOM.setObjectValue( el, value );
						} else {
							this.log.debug("[Callback Format]: Executing format...");
							var value = pimpas.DOM.getObjectValue( el );
							var vValue = "";
							var p = new Array();
							for(var i=0;i<value.length;i++)
								if(!isNaN(Number(value.charAt(i))))
									p.push(value.charAt(i));		
							var vMask = mask;
							while(vMask.indexOf('#')>=0)
								vMask = vMask.replace('#', 'xX');
							var i = 0;
							vValue = vMask;
							while(i < p.length)
								vValue = vValue.replace('xX', p[i++]);
							var t = vValue.indexOf('xX');
							if(t >=0) vValue = vValue.substring(0, t);
														
							pimpas.DOM.setObjectValue( el, vValue );
						}
						if(listener != null) {
							this.log.debug('[Callback Format]: Calling listener AFTER!');
							listener.callBack.bind(listener, pimpas.Format.time.AFTER, el, mask)();
						}
					}
				}
			);
		},
		install: function(element, cleanEvt, applyEvt, mask, listener) {
			var el = pimpas.DOM.getElement(element);
			if(!pimpas.DOM.isDefined(el)) {
				this.log.error("Can't find HTML Element with id/name ["+element+"]");
				return;
			}
			
			var id = ((el.id==null) ? el.name : el.id);
			this.log.info("[Installing Format] => Field: "+id+", Mask: "+mask+", Listener: "+listener);
			var clean = function(event) { pimpas.Format.listener.callBack(event, element, mask, listener, false) };
			var apply = function(event) { pimpas.Format.listener.callBack(event, element, mask, listener, true) };
			if(cleanEvt != null)
				pimpas.DOM.Event.addListener( el, cleanEvt, clean, true );
			pimpas.DOM.Event.addListener( el, applyEvt, apply, true );
		},
		listener: null,
		time:
			{
				BEFORE: "before",
				AFTER: "after"
			}
	}
);

pimpas.Format.initialize( );